var Skink; if (!Skink) Skink = {};

if (!Skink.Menus) Skink.Menus = {};

Skink.Menus.initAll = function() {
	air.trace('Initialising menus...');
    if (air.NativeApplication.supportsDockIcon) { // dock on OS X
        Skink.dump('Dock Menus for OS X');
        air.NativeApplication.nativeApplication.icon.menu = Skink.Menus.createRootMenu();

    } else if (air.NativeApplication.supportsSystemTrayIcon) { // system tray on windows
        Skink.dump('Making Windows system tray menu')
        air.NativeApplication.nativeApplication.icon.menu = Skink.Menus.createRootMenu();
    }

    Skink.dump('Create Native context Menus');
    Skink.Menus.contextMenu = Skink.Menus.createRootMenu();
};

Skink.Menus.onSystrayClick = function(event) {
    Skink.Windows.windowRestore();
}

// completes the loading of the systray icon
Skink.Menus.iconLoadComplete = function(event) {
    air.NativeApplication.nativeApplication.icon.bitmaps = new runtime.Array(event.target.content.bitmapData);
}

Skink.Menus.displayContextMenu = function(event) {
    Skink.dump('showing context menu');
    Skink.Menus.contextMenu.display(window.nativeWindow.stage, event.clientX, event.clientY);
};

//Reports the chosen menu command
Skink.Menus.itemSelected = function(event) {
    Skink.dump("Selected item: " + event.target.label);
    Skink.dump('event.target.name:' + event.target.name);

    if (event.target.name == "exit") {
        Skink.dump('Calling Skink.Windows.onWindowClose');
        Skink.Windows.onWindowClose();
    }

    else if (event.target.name == "reload") {
        Skink.dump('Calling Skink.Menu.menuReload');
        Skink.Menu.menuReload();
    }

    else if (event.target.name == "clear") {
        Skink.dump('Calling Skink.Menu.menuClearTimeline');
        Skink.Menu.menuClearTimeline();
    }

    else if (event.target.name == "prefs") {
        Skink.dump('Calling Skink.Menu.menuPrefs');
        Skink.Menu.menuPrefs();
    }

    else if (event.target.name == "about") {
        Skink.dump('Calling Skink.Menu.menuAbout');
        Skink.Menu.menuAbout();
    }

    else if (event.target.name == "feedback") {
        Skink.dump('Calling Skink.Menu.menuFeedback');
        Skink.Menu.menuFeedback('skink');
    }
    else if (event.target.name == "help") {
        Skink.dump('Calling Skink.Menu.menuHelp');
        Skink.Menu.menuHelp();
    }

    else {
        Skink.dump('No matching call for this menu item');
    }

}

//Creates a root-level
Skink.Menus.createRootMenu = function(type) {
    var menu = new air.NativeMenu();

    var miExit = new air.NativeMenuItem("Quit Skink");
    miExit.addEventListener(air.Event.SELECT,
    function(event) {
        Skink.Windows.windowClose();
    });
    menu.addItem(miExit);

    var miReposition = new air.NativeMenuItem("Reset position");
    miReposition.addEventListener(air.Event.SELECT,
    function(event) {
        Skink.Windows.resetPosition();
    });
    menu.addItem(miReposition);

    return menu;
}